package ph.com.gs3.formalistics.model.ldao;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.LocalDataAccessObject;
import ph.com.gs3.formalistics.model.tables.OutgoingUploadsTable;
import ph.com.gs3.formalistics.model.vo.application.OutgoingUpload;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class OutgoingUploadsLDAO extends LocalDataAccessObject {

	public static final String TAG = OutgoingUploadsLDAO.class.getSimpleName();

	public OutgoingUploadsLDAO(Context context) {
		super(context);
	}

	public void addOutgoingUpload(OutgoingUpload outgoingUpload) throws InsertFailedException {

		ContentValues cv = createCVFromOutgoingUpload(outgoingUpload);

		int insertedRows = (int) database.insert(OutgoingUploadsTable.NAME, null, cv);

		if (insertedRows <= 0) {
			throw new InsertFailedException("Failed to insert outgoing upload "
			        + outgoingUpload.getLocalFileLocation());
		} else if (insertedRows > 1) {
			FLLogger.w(TAG, "There are more than one insterted outgoing upload for "
			        + outgoingUpload.getLocalFileLocation());
		}

	}

	public List<OutgoingUpload> getAllUploadingUploads(int ownerId) {

		String whereClause = OutgoingUploadsTable.COL_OWNER_ID + " = ?";
		String[] whereArgs = { Integer.toString(ownerId) };

		Cursor cursor = database.query(OutgoingUploadsTable.NAME,
		        OutgoingUploadsTable.COLUMN_COLLECTION, whereClause, whereArgs, null, null, null,
		        null);

		cursor.moveToFirst();

		List<OutgoingUpload> outgoingUplaods = new ArrayList<>();

		while (!cursor.isAfterLast()) {
			outgoingUplaods.add(cursorToOutgoingUpload(cursor));
			cursor.moveToNext();
		}

		cursor.close();

		return outgoingUplaods;

	}

	private OutgoingUpload cursorToOutgoingUpload(Cursor cursor) {

		// @formatter:off
		int idIndex 
			= cursor.getColumnIndexOrThrow(OutgoingUploadsTable.COL_ID);
		int localFileLocationIndex 
			= cursor.getColumnIndexOrThrow(OutgoingUploadsTable.COL_LOCAL_FILE_LOCATION);
		int uploadFolderDestinationIndex 
			= cursor.getColumnIndexOrThrow(OutgoingUploadsTable.COL_UPLOAD_FOLDER_DESTINATION);
		int ownerIdIndex 
			= cursor.getColumnIndexOrThrow(OutgoingUploadsTable.COL_OWNER_ID);

		int id 							= cursor.getInt(idIndex);
		String localFileLocation 		= cursor.getString(localFileLocationIndex);
		String uploadFolderDestination 	= cursor.getString(uploadFolderDestinationIndex);
		int ownerId 					= cursor.getInt(ownerIdIndex);
		// @formatter:on

		OutgoingUpload outgoingUpload = new OutgoingUpload();

		outgoingUpload.setId(id);
		outgoingUpload.setLocalFileLocation(localFileLocation);
		outgoingUpload.setUploadFolderDestination(uploadFolderDestination);
		outgoingUpload.setOwnerId(ownerId);

		return outgoingUpload;

	}

	private ContentValues createCVFromOutgoingUpload(OutgoingUpload outgoingUpload) {

		ContentValues cv = new ContentValues();

		cv.put(OutgoingUploadsTable.COL_LOCAL_FILE_LOCATION, outgoingUpload.getLocalFileLocation());
		cv.put(OutgoingUploadsTable.COL_UPLOAD_FOLDER_DESTINATION,
		        outgoingUpload.getUploadFolderDestination());
		cv.put(OutgoingUploadsTable.COL_OWNER_ID, outgoingUpload.getOwnerId());

		return cv;

	}

}
